package com.lyc.iotdb.controller;

import com.lyc.iotdb.common.component.FlashSqlEngineTool;
import com.lyc.iotdb.common.utils.Rst;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 添加说明
 *
 * @author kisang
 * @version 1.0
 * @date 2023/11/24 11:25
 */
@Slf4j
@RestController
public class TestController {

    @javax.annotation.Resource
    FlashSqlEngineTool flashSqlEngineTool;

    /**
     * 插入数据
     *
     * @return Rst
     */
    @GetMapping("test")
    public Rst save() {

        return Rst.ok();
    }

    /**
     * 插入数据
     *
     * @return Rst
     */
    @GetMapping("testfile")
    public Rst testfile(String filepath) throws IOException {
//        List<File> list = flashSqlEngineTool.getFilesInDirectory(filepath);
//        for (File file : list) {
//            log.info("AbsolutePath:{}, {}", file.toString(), file.getAbsolutePath());
//            log.info("CanonicalPath:{}, {}", file.toString(), file.getCanonicalPath());
//        }
        // List<Path> list = getFilesInDirectory("mapper/", "xml");
        List<InputStream> list2 = getFilesInDirectory2("mapper/", "xml");
        int i = 1;
        for (InputStream file : list2) {
            byte[] buffer = new byte[1024];
            int bytesRead;

            while ((bytesRead = file.read(buffer)) != -1) {
                String fileName = "out" + i + ".xml";
                // 将数据写入本地文件
                OutputStream outputStream = new FileOutputStream(fileName);
                outputStream.write(buffer, 0, bytesRead);
                outputStream.close();
                i++;
            }
        }
        return Rst.ok();
    }

//    public List<Path> getFilesInDirectory(String directoryPath, String fileExtension) throws IOException {
//        List<Path> files = new ArrayList<>();
//        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
//        Resource[] resources = resolver.getResources("classpath*:" + directoryPath + "**/*" + fileExtension);
//
//        for (Resource resource : resources) {
//            Path path = resource.getFile().toPath();
//            files.add(path);
//        }
//        return files;
//    }

    public List<InputStream> getFilesInDirectory2(String directoryPath, String fileExtension) throws IOException {
        List<InputStream> files = new ArrayList<>();
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        Resource[] resources = resolver.getResources("classpath*:" + directoryPath + "**/*" + fileExtension);
        for (Resource resource : resources) {
            InputStream inputStream = resource.getInputStream();
            files.add(inputStream);
        }
        return files;
    }
}
